package com.meevle.commands {
	
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.meevle.business.UserDelegate;
	import com.meevle.error.ErrorHandler;
	import com.meevle.events.GetUserEvent;
	import com.meevle.model.ViewModelLocator;
	import com.meevle.vo.UserVO;
	
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	
	public class GetUserCommand implements ICommand, IResponder {
		
		public function GetUserCommand() {
			
		}
		
		// ICommand interface method
		public function execute(event:CairngormEvent):void {
			var getUserEvent:GetUserEvent = event as GetUserEvent;
			var requestedUser:UserVO = new UserVO();
			requestedUser.username = getUserEvent.username;
			
			var delegate:UserDelegate = new UserDelegate(this);
			delegate.getUser( requestedUser );
		}
		
		//IResponder interface methods
		public function result(event:Object) : void {
				var userData:UserVO = event.result as UserVO;
				ViewModelLocator.getInstance().currentUser = userData;
				ViewModelLocator.getInstance().error = null;
		}
		
		public function fault(event:Object) : void {
			ErrorHandler.getInstance().handleError(event as FaultEvent);
		}	
	}
}